<?php
defined('_JEXEC') or defined('_VALID_MOS') or die('...Direct Access to this location is not allowed...');
/**
 * @copyright Copyright (C) 2009 Joobi Limited All rights reserved.
 * @license This file is released under the GPL license (http://www.gnu.org/licenses )
 * @link http://www.ijoobi.com
 */

class aca_module
{

    var $shownamefield = 0;
    var $receivehtmldefault = 1;
    var $showreceivehtml = 0;
    var $listIds = null;
    var $linear = 0;
    var $fieldsize = 10;
    var $introtext = null;
    var $redirectURL = null;
    var $showListName = 0;
    var $buttonUnregistered = _ACA_MOD_SUBSCRIBE;
    var $imgUnregistered = null;
    var $buttonRegistered = _ACA_CHANGE_SUBSCRIPTIONS;
    var $imgRegistered = null;
    var $moduleclass_sfx = null;
    var $mod_align = null;
    var $posttext = null;
    var $defaultchecked = 1;
    var $notifType = null;
    var $catId = null;

    var $lists = null;

    var $_content = null;
    var $_html = null;
    //to be able to show more than one module in the same page
    var $num = 0;

    function aca_module()
    {
        static $num = 0;
        $this->num = ++$num;
    }

    function normal($params)
    {

        //$headerAcajoom = '<link rel="stylesheet" href="' . ACA_JPATH_LIVE. '/components/com_acajoom/css/acajoom.css" type="text/css" />';
        //$main frame->addCustom HeadTag($headerAcajoom);
        $this->shownamefield = $params->get('shownamefield', 0);
        $this->receivehtmldefault = $params->get('receivehtmldefault', 1);
        $this->showreceivehtml = $params->get('showreceivehtml', 0);
        $this->listIds = $params->get('listids', 0);
        $this->linear = $params->get('linear', 0);
        $this->fieldsize = $params->get('fieldsize', 10);
        $this->introtext = $params->get('introtext', '');
        $this->redirectURL = str_replace('&', '&amp;', $params->get('red_url', ''));
        $this->showListName = $params->get('showlistname', 1);
        $this->buttonUnregistered = $params->get('button_text', _ACA_MOD_SUBSCRIBE);
        $this->imgUnregistered = $params->get('button_img', null);
        $this->buttonRegistered = $params->get('button_text_change',
            _ACA_CHANGE_SUBSCRIPTIONS);
        $this->imgRegistered = $params->get('button_img_change', null);
        $this->moduleclass_sfx = $params->get('moduleclass_sfx', '');
        $this->mod_align = $params->get('mod_align', '');
        $this->posttext = $params->get('posttext', '');
        $this->defaultchecked = $params->get('defaultchecked', 1);
        $this->dropdown = $params->get('dropdown', 0);
        $this->selecteddrop = intval($params->get('selecteddrop', 0));

        $this->lists = lists::getSpecifiedLists($this->listIds);

        //	$this->_html = '<!--  Beginning Module : '.acajoom::version().'   -->'."\n\r";
        $this->_html .= $this->create();
        //	$this->_html .= '<!--  End Module : '.acajoom::version().'   -->'."\n\r";
        $this->_html .= acajoom::noShow();

        return $this->_html;

    }
    function create()
    {

        if (ACA_CMSTYPE) { // joomla 15
            $my = &JFactory::getUser();
        } else { //joomla 1x
            global $my;
        } //endif

        $Itemid = $GLOBALS[ACA . 'itemidAca'];
        if (!empty($Itemid)) {
            $item = '&Itemid=' . $Itemid;
        } else {
            $item = '';
        }

        $hidden = '';
        $htmlOK = false;
        $h = '';

        if (!empty($this->lists)) {
            if ($my->id > 0) {
                $loggedin = true;
                $subscriber = subscribers::getSubscriberInfoFromUserId($my->id);
                if (empty($subscriber)) {
                    subscribers::syncSubscribers(true);
                    $subscriber = subscribers::getSubscriberInfoFromUserId($my->id);
                }
                if (empty($subscriber))
                    $loggedin = false;
            } else {
                $loggedin = false;
            }

            if (!$loggedin and $GLOBALS[ACA . 'allow_unregistered']) {


                $h .= '<script type="text/javascript">
	$(document).ready(function(){
	
    check_form=function(){

	   	if($(\'#wz_11\').val() == ""){
            $(\'#alert\').html(\'Udfyld venligst navn\');
            $(\'#backoverlay\').show();
			$(\'#show_popup\').show();
			$(\'#f_focus\').html(\'wz_11\');
			return false;	
		}
        
       if($(\'#wz_12\').val() == ""){
			$(\'#alert\').html(\'Udfyld venligst e-mail\');
			$(\'#backoverlay\').show();
			$(\'#show_popup\').show();
			$(\'#f_focus\').html(\'wz_12\');
			return false;	
          }      
        else{
                if(!isEmail($(\'#wz_12\').val())){
                    $(\'#alert\').html(\'Venligst e-mail er gyldig\');
                    $(\'#backoverlay\').show();
                    $(\'#show_popup\').show();
                    $(\'#s_focus\').html(\'wz_12\');
                    return false; 
                }
          }
   
        $("#send_login").click();
        
        return true;
	};
    
      isEmail=function(string) {
		if (string.search(/^\w+((-\w+)|(\.\w+)|(\_\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
			return true;
		else return false;
    };    
    });

</script>';
                $h .= '
				<script language="javascript" type="text/javascript">
				<!--
					function submitacajoommod' . $this->num . '(formname) {
						var form = eval(\'document.\'+formname);' .
                    'var place = form.email.value.indexOf("@",1);' .
                    'var point = form.email.value.indexOf(".",place+1);';

                if ($this->shownamefield) {
                    $h .= '
						if (form.name.value == "" || form.name.value == "' . addslashes(_ACA_NAME) .
                        '") {
							alert( "' . addslashes(_ACA_REGWARN_NAME) . '" );' . 'return false;
						} else 
				';
                }
                $h .= ' if (form.email.value == "" || form.email.value == "' . addslashes(_ACA_EMAIL) .
                    '") {' . 'alert( "' . addslashes(_ACA_REGWARN_MAIL) . '" );' . 'return false;
						} else {' . 'if ((place > -1)&&(form.email.value.length >2)&&(point > 1)){' .
                    'form.submit();' . 'return true;
							} ' . 'else {' . 'alert( "' . addslashes(_ACA_REGWARN_MAIL) . '" );' .
                    'return false;' . '}' . '}' . '}
				 //-->
				</script>';
            }
            echo '<div class="w739 float_right">';
            $linkForm = '.php?option=com_acajoom';
            compa::completeLink($linkForm, false);

            $h .= '<div class="tt p5t">' . $this->introtext . '</div>';
            $h .= '<div class="p5t">';
            $h .= '<div class="p10l p15t p30b mh320">';


            if (!$loggedin) {

                if ($GLOBALS[ACA . 'allow_unregistered']) {
                    $db = JFactory::getDBO();
                    $db->setQuery('SELECT introtext FROM #__content WHERE id = 15');
                    $h .= $db->loadResult();
                    //	$h .= acajoom::printLine($this->linear, $temp);
                    $h .= '<form action="' . $linkForm . '" method="post" name="modacajoomForm' . $this->
                        num . '">';

                    $h .= '<div class="shadow_bottom">';

                    $h .= '<div class="box_nyhed shadow_top">';

                    $text = '<label>Navn*</label><input class="nyhed_txt" id="wz_11" type="text" name="name" />';
                    $h .= acajoom::printLine($this->linear, $text);

                    $text = '<label>Email</label><input class="nyhed_txt" id="wz_12" type="text"  name="email"  />';
                    $h .= acajoom::printLine($this->linear, $text);

                    $h .= '<div style="padding-left:100px"><a onclick="check_form(); "class="bt_send1" href="#"></a></div>';
                    $h .='<input type="submit" value="send_login" id="send_login" name="submit" style="display:none;" />';

                    $h .= '
						<input type="hidden" name="act" value="subscribe" />
						<input type="hidden" name="redirectlink" value="' . $this->redirectURL .
                        '" />
						<input type="hidden" name="listname" value="' . $this->showListName .
                        '" />
						</div>
                    </div>
					';


                    $h .= $hidden . '</form>';
                }
            } else {
                $h .= "Denne konto er registreret med e-mail: <strong>" . $my->email .
                    "</strong>";
            }
            //  $h .= '<input type="hidden" name="Itemid" value="'.$Itemid.'" />';

        }
        $h .= '</div></div></div><div class="cb"></div>';

        return $h;

    }


}
